/**
 * @file    yd1363_app.c
 * @brief   YDT1636协议-用户接口层
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-05-12
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-05-12 <td>1.0.1     <td>Liuwei     <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "yd1363_data.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief
 * @param
 * @return
*/
uint8_t ydt1363_com_analysis(uint8_t *rx_buf ,uint16_t rx_num , ydt1363_data_type *recv_data)
{
    uint16_t calc_crc = 0;
    uint16_t recv_crc = 0;
    uint16_t i = 0;
    uint16_t idx = 0;
    if((SOI == rx_buf[0]) && (EOI == rx_buf[rx_num - 1]) && (rx_num >= 10))
    {
        calc_crc = yd1636_calc_crc(&rx_buf[1], rx_num - 6);
        recv_crc = ascii_to_half_word(&rx_buf[rx_num - 5]);
        if(calc_crc == recv_crc)
        {
            recv_data->soi = SOI;
            recv_data->ver = ascii_to_byte(&rx_buf[1]);
            recv_data->addr = ascii_to_byte(&rx_buf[3]);
            recv_data->cid1 = ascii_to_byte(&rx_buf[5]);
            recv_data->cid2 = ascii_to_byte(&rx_buf[7]);
            recv_data->len = ascii_to_half_word(&rx_buf[9]);
            recv_data->info = ascii_to_half_word(&rx_buf[11]);
            if(check_len_vaild(recv_data->len) == TRUE)
            {
                for(i = 0;i < (recv_data->len & 0xfff); i+=2)
                {
                    recv_data->data_buf[idx++] = ascii_to_byte(&rx_buf[13 + i]);
                }
            }
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return FALSE;
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


